﻿namespace Vdc.Grid.Design
{
    using Vdc.Grid;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Globalization;

    internal class GridExportingSettingsConverter : ExpandableObjectConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                string str = (string) value;
                try
                {
                    string[] strArray = str.Split(new char[] { ',' });
                    return new GridExportingSettings { AppendTimeStamp = bool.Parse(strArray[0].Trim()), ColumnsToExport = strArray[1].Trim(), Encoding = (GridExportEncoding) int.Parse(strArray[2].Trim()), ExportAllPages = bool.Parse(strArray[3].Trim()), ExportColumnsFooter = bool.Parse(strArray[4].Trim()), ExportDetails = bool.Parse(strArray[5].Trim()), ExportedFilesLifeTime = int.Parse(strArray[6].Trim()), ExportedFilesTargetWindow = (ExportedFilesTargetWindowType) int.Parse(strArray[7].Trim()), ExportGroupFooter = bool.Parse(strArray[8].Trim()), ExportGroupHeader = bool.Parse(strArray[9].Trim()), ExportHiddenColumns = bool.Parse(strArray[10].Trim()), ExportTemplates = bool.Parse(strArray[11].Trim()), FileName = strArray[12].Trim(), KeepColumnSettings = bool.Parse(strArray[13].Trim()) };
                }
                catch (Exception exception1)
                {
                    if (exception1 != null)
                    {
                    }
                }
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return "";
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
        {
            return new GridExportingSettings { AppendTimeStamp = (bool) propertyValues["AppendTimeStamp"], ColumnsToExport = propertyValues["ColumnsToExport"].ToString(), Encoding = (GridExportEncoding) propertyValues["Encoding"], ExportAllPages = (bool) propertyValues["ExportAllPages"], ExportColumnsFooter = (bool) propertyValues["ExportColumnsFooter"], ExportDetails = (bool) propertyValues["ExportDetails"], ExportedFilesLifeTime = (int) propertyValues["ExportedFilesLifeTime"], ExportedFilesTargetWindow = (ExportedFilesTargetWindowType) propertyValues["ExportedFilesTargetWindow"], ExportGroupFooter = (bool) propertyValues["ExportGroupFooter"], ExportGroupHeader = (bool) propertyValues["ExportGroupHeader"], ExportHiddenColumns = (bool) propertyValues["ExportHiddenColumns"], ExportTemplates = (bool) propertyValues["ExportTemplates"], FileName = propertyValues["FileName"].ToString(), KeepColumnSettings = (bool) propertyValues["KeepColumnSettings"] };
        }

        public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}

